package outOfBoundException;

// 自定义的IndexOutOfBoundsException异常类
class IndexOutOfBoundException extends Exception {
    public IndexOutOfBoundException(String message) {
        super(message);
    }
}

// 学生类
class Student {
    private final String name;

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}


class Classroom {
    private final Student[] students;
    private int size;

    public Classroom(int capacity) {
        students = new Student[capacity];
        size = 0;
    }

    public void add2List(Student s, int index) throws IndexOutOfBoundException {

        if (index < 0 || index > size) {
            throw new IndexOutOfBoundException("Index: " + index + ", Size: " + size);
        }

        for (int i = size; i > index; i--) {
            students[i] = students[i - 1];
        }
        students[index] = s;
        size++;
    }


    public void printStudents() {
        for (int i = 0; i < size; i++) {
            System.out.println("Student " + (i + 1) + ": " + students[i].getName());
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Classroom classroom = new Classroom(5); // 创建一个容量为5的班级

        try {
            classroom.add2List(new Student("Alice"), 0);
            classroom.add2List(new Student("Bob"), 1);
            classroom.add2List(new Student("Charlie"), 2);
            classroom.add2List(new Student("David"), 3);
            classroom.add2List(new Student("Eve"), 4);
            classroom.add2List(new Student("Frank"), 5); // 这将抛出异常，因为索引超出范围

            classroom.printStudents();
        } catch (IndexOutOfBoundException e) {
            System.out.println("Caught an exception: " + e.getMessage());
        }
    }
}